package com.fc.test.util.wx.Utils;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.Map.Entry;

/**
 * xml工具类
 */

/**
 * @author 24733
 *
 */
public class XMLUtil {

	/**
	 * 解析xml,返回第一级元素键值对。如果第一级元素有子节点，则此节点的值是子节点的xml数据。
	 * @param strxml
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	public static Map<String, Object> doXMLParse(String strxml) throws JDOMException, IOException {
		if (null == strxml || "".equals(strxml)) {
			return null;
		}
		Map<String, Object> m = new HashMap<>();
		InputStream in = HttpClientUtil.String2Inputstream(strxml);
		SAXBuilder builder = new SAXBuilder();
		builder.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);
		builder.setFeature("http://xml.org/sax/features/external-general-entities", false);
		builder.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
		Document doc = builder.build(in);
		Element root = doc.getRootElement();
		List<?> list = root.getChildren();
		Iterator<?> it = list.iterator();
		while (it.hasNext()) {
			Element e = (Element) it.next();
			String k = e.getName();
			String v = "";
			List<?> children = e.getChildren();
			if (children.isEmpty()) {
				v = e.getTextNormalize();
			} else {
				v = XMLUtil.getChildrenText(children);
			}

			m.put(k, v);
		}
		// 关闭流
		in.close();
		return m;
	}

	/**
	 * 获取子结点的xml
	 * @param children
	 * @return String
	 */
	public static String getChildrenText(List<?> children) {
		StringBuffer sb = new StringBuffer();
		if (!children.isEmpty()) {
			Iterator<?> it = children.iterator();
			while (it.hasNext()) {
				Element e = (Element) it.next();
				String name = e.getName();
				String value = e.getTextNormalize();
				List<?> list = e.getChildren();
				sb.append("<" + name + ">");
				if (!list.isEmpty()) {
					sb.append(XMLUtil.getChildrenText(list));
				}
				sb.append(value);
				sb.append("</" + name + ">");
			}
		}

		return sb.toString();
	}

	/**
	 * 获取xml编码字符集
	 * @param strxml
	 * @return
	 * @throws IOException
	 * @throws JDOMException
	 */
	public static String getXMLEncoding(String strxml) throws JDOMException, IOException {
		InputStream in = HttpClientUtil.String2Inputstream(strxml);
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(in);
		in.close();
		return (String) doc.getProperty("encoding");
	}

	/**
	 * @param strxml
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	
	/**
	* @描述	TODO(这里用一句话描述这个方法的作用)
	* @作者	易涛
	* @时间	2018年12月5日 上午10:07:29
	* @param infoMap
	* @return	参数
	*/
	public static String getMapToXML(SortedMap<String, String> infoMap) {
		StringBuilder xml = new StringBuilder();
		xml.append("<xml>\n");
		Set<Entry<String,String>> entry1=infoMap.entrySet();
		Iterator<Entry<String,String>> it=entry1.iterator();
		while(it.hasNext()) {
			Entry<String,String> entry=it.next();
			if ("detail".equals(entry.getKey())) {
				xml.append("<" + entry.getKey() + "><![CDATA[").append(entry.getValue()).append("]]></" + entry.getKey() + ">\n");
			} else {
				xml.append("<" + entry.getKey() + ">").append(entry.getValue()).append("</" + entry.getKey() + ">\n");
			}
		}
		xml.append("</xml>");
		System.out.println(xml.toString());
		return xml.toString();
	}
	
    /**
    * @描述	支付成功，返回微信那服务器
    * @作者	易涛
    * @时间	2018年12月5日 上午10:08:40
    * @param return_code
    * @param return_msg
    * @return	参数
    */
	public static String setXML(String return_code, String return_msg) {
		String result =	"<xml>" + 
						"	<return_code>"	+ 
						"		<![CDATA[" + return_code + "]]>" + 
						"	</return_code>"	+ 
						"	<return_msg>"	+ 
						"		<![CDATA[" + return_msg + "]]>"	+		
						"	</return_msg>"	+ 
						"</xml>";
		
		return result;
	}

}
